package com.itwxz.system.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwxz.system.domain.po.WxzTool;
import com.itwxz.system.domain.vo.WxzToolMenuVo;
import com.itwxz.system.mapper.WxzToolMapper;
import com.itwxz.system.service.IWxzToolService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 工具网站树Service业务层处理
 *
 * @author gastronome
 * @date 2025-04-30
 */
@Service
@RequiredArgsConstructor
public class WxzToolServiceImpl extends ServiceImpl<WxzToolMapper, WxzTool> implements IWxzToolService {
    private final WxzToolMapper wxzToolMapper;

    /**
     * 查询工具网站树列表
     *
     * @param wxzTool 工具网站树
     * @return 工具网站树
     */
    @Override
    public List<WxzTool> selectWxzToolList(WxzTool wxzTool) {
        return wxzToolMapper.selectWxzToolList(wxzTool);
    }

    @Override
    public List<WxzToolMenuVo> getMenuWithSites() {
        List<WxzTool> all = list();
        return all.stream()
                .filter(tool -> "C".equals(tool.getType()))
                .map(menu -> {
                    WxzToolMenuVo vo = new WxzToolMenuVo();
                    BeanUtils.copyProperties(menu, vo);
                    List<WxzTool> sites = all.stream()
                            .filter(site -> "T".equals(site.getType()) && menu.getId().equals(site.getParentId()))
                            .collect(Collectors.toList());
                    vo.setSites(sites);
                    return vo;
                })
                .collect(Collectors.toList());
    }
}
